package com.lovo.bbs.servlet.admin;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lovo.bbs.bo.ForumBo;
import com.lovo.bbs.bo.ForumStatBo;
import com.lovo.bbs.vo.ForumStatVo;
import com.lovo.bbs.vo.ForumVo;

/**
 * 管理论坛
 */
public class ForumUpdateServlet extends HttpServlet {
	private static final long serialVersionUID = -5809942687822817811L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//论坛名
		String forumName = new String(req.getParameter("forumname").getBytes(
				"ISO-8859-1"), "utf-8");
		//论坛简介
		String aboutForum = new String(req.getParameter("forumread").getBytes(
				"ISO-8859-1"), "utf-8");
		String asBoardid = req.getParameter("boardid");
		int boardID = 0;//板块ID
		if (!"".equals(asBoardid) && asBoardid != null) {
			boardID = Integer.parseInt(asBoardid);
		}
		String asForumid = req.getParameter("forumid");
		int forumID = 0;//论坛ID
		if (!"".equals(asForumid) && asForumid != null) {
			forumID = Integer.parseInt(asForumid);
		}
		// 操作类型
		int opType = Integer.parseInt(req.getParameter("opType"));
		// 原页码,返回原页面时使用
		int queryPage = Integer.parseInt(req.getParameter("queryPage"));
		if (queryPage == 0) {
			queryPage = 1;
		}

		// 包装Vo
		ForumVo vo = new ForumVo();
		vo.setForumName(forumName);
		vo.setAboutForum(aboutForum);
		vo.setBoardID(boardID);
		vo.setForumID(forumID);

		int dataChanged = 0;
		ServletContext context = req.getSession().getServletContext();
		ForumBo forumBo = new ForumBo();
		ForumStatBo forumStatBo = new ForumStatBo();
		if (opType == 1) {// 增加论坛
			dataChanged = forumBo.updateForum(vo, ForumBo.OPER_OF_INSERT);
		} else if (opType == 2) {// 修改论坛
			dataChanged = forumBo.updateForum(vo, ForumBo.OPER_OF_UPDATE);
		} else if (opType == 3) {// 删除论坛
			dataChanged = forumBo.updateForum(vo, ForumBo.OPER_OF_DELETE);
			// 网站杂项信息数据库更新
			dataChanged = forumStatBo.reset();
		}
		
		// 取得所有论坛列表
		ArrayList<ForumVo> forumList = forumBo.getAllForum();
		// 更新论坛列表
		context.setAttribute("forumList", forumList);
		// 更新论坛MAP
		context.setAttribute("forumMap", forumBo.getForumMap(forumList));

		
		if (opType == 3) {// 如果是删除论坛,则同时更新......
			// 更新网站杂项信息
			ForumStatVo forumInfo = forumStatBo.getForumInfo();
			context.setAttribute("forumInfo", forumInfo);
		}

		if (dataChanged == 1) {
			resp
					.sendRedirect("forumManageQueryPage.jsp?queryPage="
							+ queryPage);
		}
	}

}
